package com.example.event_resource_java.commons.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Description: 时间工具类
 * @Author: ak
 * @CreateDate: 2024/4/23 11:49
 * @UpdateUser:
 * @UpdateDate: 2024/4/23 11:49
 * @UpdateRemark:
 * @Version: 1.0
 */

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String dateTimeHm(final Date date) {
        return parseDateToStr(YYYY_MM_DD_HH_MM, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取时间时分秒
     *
     * @param time
     * @return
     */
    public static final String getHourMinuteSecond(String time) {
        DateTime dateTime = DateUtil.parseDateTime(time);
        StringBuffer sb = new StringBuffer();
        int hourMinute = dateTime.hour(true) * 60 + dateTime.minute();

        sb.append(hourMinute).append(".");

        if (dateTime.second() < 10) {
            sb.append("0").append(dateTime.second());
        } else if (dateTime.second() == 0) {
            sb.append("00");
        } else {
            sb.append(dateTime.second());
        }
        return sb.toString();
    }

    public static final String getTimeLong(String time) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = format.parse(time); //转换成格林威治时间
        Long timeLongNum = date.getTime(); //转换为时间戳
        return String.valueOf(timeLongNum);
    }

//    public static final String minuteFormat(Long duration) throws ParseException {
//        long minute = duration % 60;
//        Long hour = duration / 60;
//        StringBuffer sb = new StringBuffer();
//        sb.append("1970-01-01 ");
//        if (minute < 9) {
//            sb.append("0").append(hour);
//        }else {
//            sb.append(hour);
//        }
//        sb.append(":");
//        if (minute < 9) {
//            sb.append("0").append(minute);
//        }else {
//            sb.append(minute);
//        }
//        sb.append(":").append("00");
//        return sb.toString();
//
//    }
}
